package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;



    //每天凌晨2点，查询前一天日期统计数据，吧统计之后数据添加统计结果表里面
    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(cron = "0/10 * * * * ? ")  //TODO 测试用,每十秒一次
    public void orderTotalAmountStatistics(){
        System.out.println(new Date().toInstant());
        //获取前一天日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        //根据前一天日期进行统计功能
        //统计前一天的交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        if(orderStatistics != null) {
            //把统计之后的数据，添加统计结果表里面
            orderStatisticsMapper.insert(orderStatistics) ;
        }
    }
}
